﻿using System;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Samurai.Wakizashi.Caching;

// ReSharper disable InconsistentNaming
namespace Samurai.Caching.Tests
{
    [TestClass]
    public class ExpiringCacheStore_Tests
    {
        private bool _actionExecuted;

        [TestMethod]
        public void Evicts_cache_at_designated_expiration_period()
        {
            var cache = new ExpiringCacheStore<CacheItem>
                            {
                                ExpirationTimeSpan = TimeSpan.FromMilliseconds(500)
                            };

            cache.CacheItem(new CacheItem());

            Thread.Sleep(800);

            Assert.IsTrue(cache.AllItems.Count() == 0);
        }

        [TestMethod]
        public void Executes_specified_action_at_designated_expiration_period()
        {
            var cache = new ExpiringCacheStore<CacheItem>
            {
                ExpirationTimeSpan = TimeSpan.FromMilliseconds(500),
                ExpireAction = ExpirationAction
            };

            // Expiration timer will not begin until we add the first item
            cache.CacheItem(new CacheItem());

            Thread.Sleep(800);

            Assert.IsTrue(_actionExecuted);
        }

        private void ExpirationAction(IExpiringCacheStore cache)
        {
            _actionExecuted = true;
        }
    }
}
// ReSharper restore InconsistentNaming